package com.alan.wifishare.view.impl.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.alan.libapi.model.business.UserSession;
import com.alan.libapi.test.ITest;
import com.alan.library.allog.KLog;
import com.alan.wifishare.R;
import com.alan.wifishare.app.GlobalField;
import com.alan.wifishare.utils.BusinessUtil;
import com.alan.wifishare.view.impl.fragment.CNTHaveConnectedFM;
import com.alan.wifishare.view.impl.fragment.ChangeFragmentListener;
import com.alan.wifishare.view.impl.fragment.MainConnectFragment;
import com.alan.wifishare.view.impl.fragment.MainMyFragment;
import com.alan.wifishare.view.impl.fragment.MainShareFragment;
import com.alan.wifishare.view.impl.service.BusinessService;
import com.alan.wifishare.view.impl.service.NetworkManagerService;

//AppCompatActivity
public class MainActivity extends FragmentActivity {

    private final static String TAG = "MainActivity";
    private static final String LOG_MSG = "KLog is a so cool Log Tool!";
    private MainConnectFragment mainConnectFM;
    private Fragment mainShareFM, mainMyFM, cntHaveConnectedFM;
    private Fragment curFragment;
    private RadioGroup radioGroup;
    private int FRAGMENT_CHANGE_FLAG_TO_CONNECT = 1;
    private int FRAGMENT_CHANGE_FLAG_HAVE_CONNECTED = 2;
    private ChangeFragmentListener changeFragmentListener = new ChangeFragmentListener() {
        @Override
        public void onFragmentChange(int flag) {

        }
    };
    private ITest test;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        test = new TestGetAPI();
//        test = new TestPostAPI();
//        test = new TestTcpAPI();
//        test.start();
        UserSession userSession = new UserSession();
        userSession.setUserId(9);
        userSession.setSession("ecde35356a78db69a97de64d12ef4f52");
        GlobalField.userSession = userSession;
        initView();
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(test != null){
            test.finish();
        }
        NetworkManagerService.stopService(GlobalField.context);
        if(BusinessUtil.isConnectedLink()){
            BusinessService.stopService(this);
        }
        System.exit(0);
    }


    private void initView() {
        radioGroup = (RadioGroup)findViewById(R.id.tabMainRg);
        RadioButton radioButton = (RadioButton)findViewById(R.id.tabMainConnectRb);
        radioButton.setChecked(true);
        setTabSelection(0);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.tabMainConnectRb:
                        setTabSelection(0);
                        break;
                    case R.id.tabMainShareRb:
                        setTabSelection(1);
                        break;
                    case R.id.tabMainMyRb:
                        setTabSelection(2);
                        break;
                    default:
                        break;
                }
            }
        });

    }

    private void setTabSelection(int index) {
        final FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        hiddenAllFragments(fragmentTransaction);
        switch (index) {
            case 0:
                if(BusinessUtil.isConnectedLink()){
                    if(cntHaveConnectedFM == null){
                        cntHaveConnectedFM = new CNTHaveConnectedFM();
                        fragmentTransaction.add(R.id.mainFragment, cntHaveConnectedFM);
                    }else {
                        fragmentTransaction.show(cntHaveConnectedFM);
                    }
                    curFragment = cntHaveConnectedFM;
                }else {
                    if(mainConnectFM == null){
                        mainConnectFM = new MainConnectFragment();
                        fragmentTransaction.add(R.id.mainFragment, mainConnectFM);
                        mainConnectFM.setChangeFragmentListener(new ChangeFragmentListener() {
                            @Override
                            public void onFragmentChange(int flag) {
                                KLog.e();
                                setTabSelection(0);
                            }
                        });
                    }else {
                        fragmentTransaction.show(mainConnectFM);
                    }
                    curFragment = mainConnectFM;
                }

                break;
            case 1:
                if(mainShareFM == null){
                    mainShareFM = new MainShareFragment();
                    fragmentTransaction.add(R.id.mainFragment, mainShareFM);
                }else {
                    fragmentTransaction.show(mainShareFM);
                }
                curFragment = mainShareFM;
                break;
            case 2:
                if(mainMyFM == null){
                    mainMyFM = new MainMyFragment();
                    fragmentTransaction.add(R.id.mainFragment, mainMyFM);
                }else {
                    fragmentTransaction.show(mainMyFM);
                }
                curFragment = mainMyFM;
                break;
            default:
                break;
        }
        fragmentTransaction.commit();
    }

    /**
     * 隐藏所有的Fragment
     */
    private void hiddenAllFragments(FragmentTransaction fragmentTransaction) {
        if(mainConnectFM != null){
            fragmentTransaction.hide(mainConnectFM);
        }
        if(mainShareFM != null){
            fragmentTransaction.hide(mainShareFM);
        }
        if(mainMyFM != null){
            fragmentTransaction.hide(mainMyFM);
        }
        if(cntHaveConnectedFM != null){
            fragmentTransaction.hide(cntHaveConnectedFM);
        }
    }

    public void toastMsgShort(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        curFragment.onActivityResult(requestCode, resultCode, data);
    }
}
